package com.yksj.healthtalk.utils;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;

import com.yksj.healthtalk.adapter.LocationExpandAdapter;
import com.yksj.healthtalk.adapter.LocationExpandAdapter.onChildItem;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.comm.LodingFragmentDialog;
import com.yksj.healthtalk.entity.WeatherEntity;
import com.yksj.healthtalk.net.http.AsyncHttpResponseHandler;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.ui.R;

public class LocationSelectionActivity extends BaseFragmentActivity implements onChildItem, android.view.View.OnClickListener{

	  	private ExpandableListView mListView = null;
	    private LocationExpandAdapter mAdapter = null;
	    private Map<WeatherEntity,List<WeatherEntity>> map=new LinkedHashMap<WeatherEntity, List<WeatherEntity>>();

	    /** Called when the activity is first created. */
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.selection_city_layout);
	        initTitle();  initView(); 
	        initData();
	        mListView.setGroupIndicator(null);
	        mAdapter = new LocationExpandAdapter(this, map);
	        mListView.setAdapter(mAdapter);
	        mAdapter.setOnChildItemClick(this);
	        mListView.setDescendantFocusability(ExpandableListView.FOCUS_AFTER_DESCENDANTS);
	        
	    }

	    private void initData() {
	    	HttpRestClient.doHttpGetLocationData(new AsyncHttpResponseHandler(){
	    		@Override
	    		public void onSuccess(int statusCode, String content) {
	    			super.onSuccess(statusCode, content);
	    			LodingFragmentDialog.dismiss(getSupportFragmentManager());
	    			map = WeatherEntity.parseJsonToInfo(content);
	    			mAdapter.setIterator(map.keySet().iterator());
	    			mAdapter = new LocationExpandAdapter(getApplicationContext(), map);
	    			mAdapter.setOnChildItemClick(LocationSelectionActivity.this);
	    		    mListView.setAdapter(mAdapter);
	    		}
	    		
	    	});
	
}

		private void initView() {
	    	findViewById(R.id.title).setBackgroundResource(R.drawable.setting_title_bg);
	    	mListView=(ExpandableListView)findViewById(R.id.expandalistview);
	    	titleLeftBtn.setOnClickListener(this);
	    	titleTextV.setText("选择城市");
	    }

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.title_back:
				onBackPressed();
				break;
			}
			
		}
/**
 * 点击子元素的时候..
 */
		@Override
		public void onChildItemClick(WeatherEntity entity) {
			Intent intent = new Intent(this,WeatherEntity.class);
			intent.putExtra("key", entity.getAREA_CODE());
			setResult(Activity.RESULT_OK, intent);
			finish();//结束之后会将结果传回From
		}
}
